//
//  JKRFeedDetailHelper.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/9.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedDetailHelper.h"

@implementation JKRFeedDetailHelper

+ (NSBundle *)bundle {
    static NSBundle *bundle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *path = [[NSBundle mainBundle] pathForResource:@"FeedDetail" ofType:@"bundle"];
        bundle = [NSBundle bundleWithPath:path];
    });
    return bundle;
}

+ (YYMemoryCache *)imageCache {
    static YYMemoryCache *cache;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cache = [YYMemoryCache new];
        cache.shouldRemoveAllObjectsOnMemoryWarning = NO;
        cache.shouldRemoveAllObjectsWhenEnteringBackground = NO;
        cache.name = @"FeedDetailImageCache";
    });
    return cache;
}

+ (YYWebImageManager *)circleHeaderImageManager {
    static YYWebImageManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *path = [[UIApplication sharedApplication].cachesPath stringByAppendingPathComponent:@"circleHeader"];
        YYImageCache *cache = [[YYImageCache alloc] initWithPath:path];
        manager = [[YYWebImageManager alloc] initWithCache:cache queue:[YYWebImageManager sharedManager].queue];
        manager.sharedTransformBlock = ^(UIImage *image, NSURL *url) {
            if (!image) return image;
            return [image imageByRoundCornerRadius:MAXFLOAT borderWidth:2.f borderColor:UIColorHex(CCCCCC)];
        };
    });
    return manager;
}

+ (UIImage *)imageNamed:(NSString *)name {
    if (!name) return nil;
    UIImage *image = [[self imageCache] objectForKey:name];
    if (image) return image;
    NSString *ext = name.pathExtension;
    if (ext.length == 0) ext = @"png";
    NSString *path = [[self bundle] pathForScaledResource:name ofType:ext];
    if (!path) return nil;
    image = [UIImage imageWithContentsOfFile:path];
    image = [image imageByDecoded];
    if (!image) return nil;
    [[self imageCache] setObject:image forKey:name];
    return image;
}

+ (UIImage *)progressImageWithProgressTitle:(NSString *)title highLight:(BOOL)highLight{
    if (!title) {
        return nil;
    }
    if ([title isEqualToString:@"已发布"]) {
        return [JKRFeedDetailHelper imageNamed:@"yfb_1"];
    }
    if ([title isEqualToString:@"已结束"]) {
        if (highLight) {
            return [JKRFeedDetailHelper imageNamed:@"wcfw_1"];
        } else {
            return [JKRFeedDetailHelper imageNamed:@"wcfw_2"];
        }
    }
    if ([title isEqualToString:@"开始服务"] || [title isEqualToString:@"服务开始"]) {
        if (highLight) {
            return [JKRFeedDetailHelper imageNamed:@"ksfw_1"];
        } else {
            return [JKRFeedDetailHelper imageNamed:@"ksfw_2"];
        }
    }
    if ([title isEqualToString:@"选择大神"]) {
        if (highLight) {
            return [JKRFeedDetailHelper imageNamed:@"xzds_1"];
        } else {
            return [JKRFeedDetailHelper imageNamed:@"xzds_2"];
        }
    }
    if ([title isEqualToString:@"确认服务"] || [title isEqualToString:@"请求确认"]) {
        if (highLight) {
            return [JKRFeedDetailHelper imageNamed:@"qrfw_1"];
        } else {
            return [JKRFeedDetailHelper imageNamed:@"qrfw_2"];
        }
    }
    if ([title isEqualToString:@"申诉中"]) {
        if (highLight) {
            return [JKRFeedDetailHelper imageNamed:@"ssz_1"];
        } else {
            return [JKRFeedDetailHelper imageNamed:@"ssz_2"];
        }
    }
    if ([title isEqualToString:@"申诉失败"]) {
        return [JKRFeedDetailHelper imageNamed:@"ssz_2"];
    }
    if ([title isEqualToString:@"服务完成"] || [title isEqualToString:@"完成服务"]) {
        if (highLight) {
            return [JKRFeedDetailHelper imageNamed:@"wcfw_1"];
        } else {
            return [JKRFeedDetailHelper imageNamed:@"wcfw_2"];
        }
    }
    if ([title isEqualToString:@"已退款"]) {
        if (highLight) {
            return [JKRFeedDetailHelper imageNamed:@"ytk_1"];
        } else {
            return [JKRFeedDetailHelper imageNamed:@"ytk_2"];
        }
    }
    if ([title isEqualToString:@"结束服务"]) {
        if (highLight) {
            return [JKRFeedDetailHelper imageNamed:@"jsfw_1"];
        } else {
            return [JKRFeedDetailHelper imageNamed:@"jsfw_2"];
        }
    }
    return nil;
}

@end
